package saleMarket.service;

import saleMarket.dao.CommonDao;
import saleMarket.dao.WarehouseDao;
import saleMarket.domain.Warehouse;
import util.JdbcHelper;
import util.exception.ErrorException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

/**
 * @author 张方恒
 * @version 1.0  12-13-2020
 */

public class WarehouseService {
    private static WarehouseDao warehouseDao = WarehouseDao.getInstance();
    private static WarehouseService warehouseService =new WarehouseService();
    private WarehouseService(){}

    public static WarehouseService getInstance(){
        return warehouseService;
    }

    public Warehouse find(Integer id) throws SQLException {
        Connection connection= JdbcHelper.getConn();
        Warehouse result=warehouseDao.find(id,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(Warehouse warehouse) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean updated = false;
        try{
            updated = warehouseDao.update(warehouse,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Warehouse warehouse) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        Boolean added = false;
        try{
            added = warehouseDao.add(warehouse,connection);
            connection.commit();
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added ;
    }

    public boolean delete(Integer id)
            throws SQLException, ErrorException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean result=false;
        try{
            int countInventoryGoodsOfProfTitle = CommonDao.getInstance().
                    countWhereForeignKey
                            (connection,"inventoryGoods",
                                    "warehouse_id", id);
            //若没有二级单位，则能够删除
            if(countInventoryGoodsOfProfTitle==0){
                result = warehouseDao.delete(id,connection);
            }else {
                throw new ErrorException( "id为"+id+"的仓库有下属系，不能删除");
            }
        }catch (SQLException e){
            connection.rollback();
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return result;
    }
    public Collection<Warehouse> findAll()
            throws SQLException{
        Connection connection= JdbcHelper.getConn();
        Collection<Warehouse> warehouses=warehouseDao.findAll(connection);
        JdbcHelper.close(connection);
        return warehouses;
    }


}
